import org.newdawn.slick.util.pathfinding.*;

public class AStar {

	private static final int MAX_PATH_LENGTH = 100;
	AStarPathFinder pathFinder;
	Path path;

	public AStar() {

		SimpleMap map = new SimpleMap(Map.tiles);
		pathFinder = new AStarPathFinder(map, MAX_PATH_LENGTH, false);

	}

	private boolean isThere;
	public void findPath(int x1, int y1, int x2, int y2) {
		if(x1 == x2 && y1 == y2) {
			isThere = true;
		} else {
			isThere = false;
		}
		path = pathFinder.findPath(null, x1, y1, x2, y2);
	}

	public int getX(int n) {
		if(getLength() > 0) {
			return path.getX(n);
		} else {
			return -1;			
		}
	}
	public int getY(int n) {
		if(getLength() > 0) {
			return path.getY(n);
		} else {
			return -1;
		}
	}
	public int getLength() {
		if(isThere) {
			return -1;
		} else {
			return path.getLength();
		}
	}
	
	public int getAction(int x, int y, int xtarget, int ytarget) {
		findPath(x,y,xtarget,ytarget);
		
		int action = BunnyAction.NULL;

		if(getX(1) != -1 && getY(1) != -1) {
			if(getX(1) > x) {
				action = BunnyAction.RIGHT;
			} else if(getX(1) < x) {
				action = BunnyAction.LEFT;
			}
			if(getY(1) > y) {
				action = BunnyAction.DOWN;
			} else if(getY(1) < y) {
				action = BunnyAction.UP;
			}
		}
		return action;
	}
	
	public void printPath() {
		int length = getLength();
		for(int i = 0; i < length; i++) {
			System.out.println("Move to: " + getX(i) + "," + getY(i));
		}
	}

}
